﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZQNB.Common;
using ZQNB.Common.Xlios;

namespace ExcelDemo
{
    public class ShangHaiCount
    {
        public string Phase { get; set; }
        public string Subject { get; set; }
        public int Count { get; set; }
    }

    public class DicItem
    {
        public string Code { get; set; }
        public string Name { get; set; }
        public string DicTypeCode { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            XlioHelper helper = new XlioHelper();
            //helper.MakeTemplate<ShangHaiCount>("c:\\ShangHaiCount.xlsx");
            //helper.MakeTemplate<DicItem>("c:\\DicItem.xlsx");

            const string dicItemFile = "c:\\DicItem.xlsx";
            IList<DicItem> dicItems = helper.Read<DicItem>(dicItemFile);

            MakeResult(dicItems, "c:\\Expert.xlsx", "c:\\ExpertResult.xlsx", helper);
            MakeResult(dicItems, "c:\\Teacher.xlsx", "c:\\TeacherResult.xlsx", helper);

            Console.Read();
        }


        private static void MakeResult(IList<DicItem> dicItems, string file, string resultFile, XlioHelper helper)
        {
            IList<ShangHaiCount> users = helper.Read<ShangHaiCount>(file);
            FixList(ref users, dicItems);
            helper.Write(resultFile, users);
        }

        private static void FixList(ref IList<ShangHaiCount> teachers, IList<DicItem> dicItems)
        {
            foreach (var teacher in teachers)
            {
                if (teacher.Phase == "NULL")
                {
                    teacher.Phase = "无";
                }
                else
                {
                    string[] phases = MyStringHelper.SplitString(teacher.Phase);
                    IList<string> names = new List<string>();
                    foreach (var phase in phases)
                    {
                        var theOne = dicItems.Single(x => x.Code.NbEquals(phase));
                        if (theOne != null)
                        {
                            names.Add(theOne.Name);
                        }
                    }
                    teacher.Phase = MyStringHelper.MakeSplitString(names.ToArray());
                }

                if (teacher.Subject == "NULL")
                {
                    teacher.Subject = "无";
                }
                else
                {
                    string[] subjects = MyStringHelper.SplitString(teacher.Subject);
                    IList<string> names = new List<string>();
                    foreach (var subject in subjects)
                    {
                        var theOne = dicItems.SingleOrDefault(x => x.Code.NbEquals(subject));
                        if (theOne != null)
                        {
                            names.Add(theOne.Name);
                        }
                    }
                    teacher.Subject = MyStringHelper.MakeSplitString(names.ToArray());

                }
            }
        }
    }
}
